package cn.shengchao.examstar.rpc.distribute.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @program: examstarapi
 * @description: 分销角色枚举
 * @author: Xiong weiqiang
 * @create: 2025-03-25 10:22
 */
@Getter
@AllArgsConstructor
public enum DistributeSaleRoleEnum {
    ROLE_YSXG("YSXG", "平台", 0),
    ROLE_MKMG("MKMG", "市场经理", 20),
    ROLE_DSPD("DSPD", "分销商", 30),
    ROLE_DSPD_EMP("DSPD_EMP", "分销商员工", 31),
    ROLE_SLMB ("SLMB", "会员", 40);
    private final String code;
    private final String description;
    private final Integer distributeLevel;
    /**
     * 按编码获取角色
     * @param code
     * @return
     */
    public static DistributeSaleRoleEnum getByCode(final String code) {
        for (DistributeSaleRoleEnum distributeSaleRoleEnum : DistributeSaleRoleEnum.values()) {
            if (distributeSaleRoleEnum.getCode().equals(code)) {
                return distributeSaleRoleEnum;
            }
        }
        return null;
    }
    /**
     * 按等级获取角色
     * @param level
     * @return
     */
    public static DistributeSaleRoleEnum getRoleByLevel(final Integer level) {
        for (DistributeSaleRoleEnum distributeSaleRoleEnum : DistributeSaleRoleEnum.values()) {
            if (distributeSaleRoleEnum.getDistributeLevel().equals(level)) {
                return distributeSaleRoleEnum;
            }
        }
        return null;
    }

    /** 
     * method : getUpperRoleByCode
     * description :     获取上级角色，等级编码的规则是越小越上，统一体系第一个数字相同
     * @param code \{@link String \} 
     * @return : cn.shengchao.examstar.enums.DistributeSaleRoleEnum
     * @author : Xiong weiqiang 
     * @CreateDate : 2025-03-25 17:35:52
     *  
     * @Reviewed : 
     * @UpdateLog :    Name    Date    Reason/Contents
     *             ---------------------------------------
     *                ****    ****    **** 
     *
     */
    public static DistributeSaleRoleEnum getUpperRoleByCode(final String code) {
        DistributeSaleRoleEnum distributeSaleRoleEnum = DistributeSaleRoleEnum.getByCode(code);

        if (distributeSaleRoleEnum != null) {
            Integer upperLevel=0;
            Integer level = distributeSaleRoleEnum.getDistributeLevel();
            for (DistributeSaleRoleEnum roleEnum : DistributeSaleRoleEnum.values()) {
                if (roleEnum.getDistributeLevel() < level&&roleEnum.getDistributeLevel()>upperLevel) {
                    upperLevel=roleEnum.getDistributeLevel();
                }
            }
            return getRoleByLevel(upperLevel);
        }else {
            return null;
        }
    }

    /**
     * method : getUpperRoleByLevel
     * description :     获取上级角色，等级编码的规则是越小越上，统一体系第一个数字相同
     * @param level \{@link Integer \}
     * @return : cn.shengchao.examstar.enums.DistributeSaleRoleEnum
     * @author : Xiong weiqiang
     * @CreateDate : 2025-03-25 17:35:52
     *
     * @Reviewed :
     * @UpdateLog :    Name    Date    Reason/Contents
     *             ---------------------------------------
     *                ****    ****    ****
     *
     */
    public static DistributeSaleRoleEnum getUpperRoleByLevel(final Integer level) {
        DistributeSaleRoleEnum distributeSaleRoleEnum = DistributeSaleRoleEnum.getRoleByLevel(level);
        if (distributeSaleRoleEnum != null) {
            Integer upperLevel=0;
            for (DistributeSaleRoleEnum roleEnum : DistributeSaleRoleEnum.values()) {
                if (roleEnum.getDistributeLevel() < level&&roleEnum.getDistributeLevel()>upperLevel) {
                    upperLevel=roleEnum.getDistributeLevel();
                }
            }
            return getRoleByLevel(upperLevel);
        }else {
            return null;
        }
    }
}